<?php
class ASThumbnail_Action extends Typecho_Widget implements Widget_Interface_Do
{
	private $ext_arr;
	private $suffix;
	private $path;
	
	public function action(){
		$path = $this->request->get("path");
		$size = $this->request->get("size");
		
		
		echo ASThumbnail_Plugin::ROUTE_NAME.'/'.$path;

	/*
		$this->path = $this->request->get("path");
		
		
		if( $this->isThumbnail() ){
			$new_path = $this->getPath();
			$new_file = $this->getFile();	
		}else{
			$new_path = $this->getNewPath();
			$new_file = $this->getNewFile();		
		}

		if( !file_exists($new_file) ){
			if(!$this->zoomImg()){ return false; }
		}

		$options = Typecho_Widget::widget('Widget_Options');
		$this->response->redirect($options->index . $new_path);*/
	}
	
	private function getPath(){
		return $this->path;
	}
	
	private function getFile(){ /* 获取原文件磁盘路径 */
		$path = $this->getPath();
		return __TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . trim($path, '/\\');
	}
	
	private function isThumbnail(){
		$path = $this->getPath();
		$ext = $this->getExt();
		if( stripos($path, $this->suffix.$ext) ){
			return true;
		}
		return false;
	}
	
	private function getExt(){ /* 获取扩展名 */
		$path = $this->getPath();
		return strtolower(strrchr($path, '.'));
	}
	
	private function getNewPath(){
		$ext = $this->getExt();
		$path = $this->getPath();
		return str_replace($ext, $this->suffix.$ext, $path);
	}
	
	private function getNewFile(){ /* 获取新文件磁盘路径 */
		$path = $this->getNewPath();
		return __TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . trim($path, '/\\');
	}
	
	private function getImg(){ /* 获取图片信息 */
		$ext = $this->getExt();
		$file = $this->getFile();
		if( !file_exists($file) ){return false; }
		if( !in_array($ext, $this->ext_arr) ){ return false; }
		return @getimagesize($file);
	}
	
	private function getImgData(){
		$file = $this->getFile();
		if( !$this->getImg() ){ return false; }
		return  base64_encode( file_get_contents($file) );
	}
	
	private function zoomImg(){
		if(!$this->getImg()){ return false; }
		list($W, $H) = $this->getImg();
		$w = defined('___MAX_W___') ? ___MAX_W___ : 200; 
		$h = defined('___MAX_H___') ? ___MAX_H___ : 200; 
		$rate = $W/$H;
		if($W >= $H){
			$_W = $w;
			$_H = round($_W/$rate);
		}else{
			$_H = $h;
			$_W = round($_H*$rate);
		}

		if( !$this->getImgData() ){ return false; }
		$data = $this->getImgData();
		$new_path = $this->getNewFile();
		
		$thumb = @imagecreatetruecolor($_W, $_H);
		$im = @imagecreatefromstring(base64_decode($data));
		@imagecopyresized($thumb, $im, 0, 0, 0, 0, $_W, $_H, $W, $H);

		// Output
		if( @imagejpeg($thumb, $new_path) ){
			return true;
		}else{
			return false;
		}
	}
}
